package com.wtwd.campus.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.service.*;
import com.wtwd.campus.utils.GetCurrentSysUserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/06/17
 * @description
 */
@RestController
@RequestMapping("/select")
public class SelectController {

    private static final Logger logger = LoggerFactory.getLogger(SelectController.class);
    @Autowired
    public RedisService redisService;
    @Autowired
    public DeviceInfoService deviceInfoService;
    @Autowired
    public GradeService gradeService;
    @Autowired
    public SchoolService schoolService;
    @Autowired
    public ClassesService classesService;
    @Autowired
    public GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;

    /**
     * 通过组织code模糊查询未绑定的imei号
     *
     * @param imei
     * @return
     */
    @GetMapping("/getImei/{imei}")
    public RespEntity getImei(@PathVariable String imei) {
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        List<Map<String, Object>> deviceMapList = deviceInfoService.getDeviceImeiAndDeviceIdByOrgCodeLikeImei(imei, sysUser.getOrgCode());
        return new RespEntity(RespCode.SUCCESS, deviceMapList);
    }

    /**
     * 通过学校id获取年级id以及年级名称
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/getGrade/{schoolId}")
    public RespEntity getGrade(@PathVariable Integer schoolId) {
        logger.info("getGrade : param=" + schoolId);
        List<Map<String, Object>> gradeMapList = gradeService.getGradeIdAndGradeNameBySchoolId(schoolId);
        return new RespEntity(RespCode.SUCCESS, gradeMapList);
    }

    /**
     * 通过年级id获取班级
     *
     * @param gradeId
     * @return
     */
    @GetMapping("/getClasses/{gradeId}")
    public RespEntity getClasses(@PathVariable Integer gradeId) {
        logger.info("getClasses : param=" + gradeId);
        List<Map<String, Object>> classesMapList = classesService.getClassesIdAndClassNameByGradeId(gradeId);
        return new RespEntity(RespCode.SUCCESS, classesMapList);
    }

    /**
     * 根据用户获取设备型号
     * @return
     */
    @GetMapping("/getModel")
    public  RespEntity getModel(){
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        List<Map<String, Object>> modelList = deviceInfoService.getModelByOrgCode(sysUser.getOrgCode());
        return new RespEntity(RespCode.SUCCESS,modelList);
    }

}
